package com.tps.cloud.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tps.cloud.response.Result;
import com.tps.cloud.system.dto.SystemDictDto;
import com.tps.cloud.system.dto.SystemDictItemDto;
import com.tps.cloud.system.param.SystemDictItemParam;
import com.tps.cloud.system.param.SystemDictParam;
import com.tps.cloud.system.service.SystemDictItemService;
import com.tps.cloud.system.vo.SystemDictItemVo;
import com.tps.cloud.system.vo.SystemDictVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典数据表(SystemDictItem)表控制层
 */
@RestController
@AllArgsConstructor
@RequestMapping("/system/dict_item")
@Tag(name = "字典项管理", description = "用于管理字典项")
@Slf4j
public class SystemDictItemController {

    private final SystemDictItemService systemDictItemService;

    /**
     * 分页查询字典类型
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("/page")
    @Operation(summary = "字典项分页查询", description = "字典项分页查询")
    public Result<IPage> page(@ParameterObject Page page, @ParameterObject SystemDictItemParam systemDictItemParam) {
        return Result.ok(systemDictItemService.selectDictItemPage(page,systemDictItemParam));
    }


    @PostMapping
    @Operation(summary = "创建字典项", description = "创建字典项")
    public Result<Long> save(@RequestBody @Validated SystemDictItemDto systemDictItemDto) {
        Long id=systemDictItemService.createDictItem(systemDictItemDto);
        return Result.ok(id);
    }

    @PutMapping
    @Operation(summary = "更新字典项", description = "更新字典项")
    public Result<Boolean> update(@RequestBody @Validated SystemDictItemDto systemDictItemDto) {
        systemDictItemService.updateDictItem(systemDictItemDto);
        return Result.ok(true);
    }

    @DeleteMapping
    @Operation(summary = "删除字典项", description = "根据字典id删除字典项")
    @Parameter(name = "ids", description = "字典项id集合", required = true, example = "1024")
    public Result<Boolean> delete(@RequestBody List<Long> ids) {
        systemDictItemService.deleteDictItemByIds(ids);
        return Result.ok(true);
    }

    @GetMapping(value = "/get/{id}")
    @Operation(summary = "/查询字典项详细")
    @Parameter(name = "id", description = "主键id", required = true, example = "1024")
    public Result<SystemDictItemVo> get(@PathVariable Long id) {
        SystemDictItemVo systemDictItemVo = systemDictItemService.getDictItem(id);
        return Result.ok(systemDictItemVo);
    }


}

